﻿using System;
using System.Collections.Generic;
using System.Text;
using log4net;

namespace RollBackLib.src.core
{
    /// <summary>
    /// Represents a single email id entry.
    /// </summary>
    public class EmailEntry
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(EmailEntry));

        public string m_EmailId
        {
            get;
            set;
        }

        public string m_Password
        {
            get;
            set;
        }

        /// <summary>
        /// The un-encrypted form of the password
        /// </summary>
        private string m_unencryptedPassword;

        /// <summary>
        /// The key into the SMTP server map
        /// </summary>
        public string m_keyOfSmtpServerList
        {
            get;
            set;
        }

        public bool m_Enabled
        {
            get;
            set;
        }

        public EmailEntry()
        {
        }

        public EmailEntry(string emailId, string password, string keyOfSmtpServerList)
        {
            m_EmailId = emailId;
            m_Password = password;
            m_keyOfSmtpServerList = keyOfSmtpServerList;
        }

        internal string getKey()
        {
            return m_EmailId;
        }

        /// <summary>
        /// Call this function to save the un-encrypted password into a private member
        /// field and copy the encrypted value into the m_password field.
        /// </summary>
        /// <param name="encrypted"></param>
        public void setEncryptedPassword(String encrypted)
        {
            m_unencryptedPassword = m_Password;
            m_Password = encrypted;
        }

        /// <summary>
        /// Revert the affect of the setEncryptedPassword
        /// </summary>
        public void restoreUnEncryptedPassword()
        {
            m_Password = m_unencryptedPassword;
            m_unencryptedPassword = "";
        }
    }

    /// <summary>
    /// List of email ids which gets serialized to an XML file.
    /// </summary>
    public class EmailTable
    {
        /// <summary>
        /// The list of email id entries
        /// </summary>
        public List<EmailEntry> m_EmailList = new List<EmailEntry>();

        /// <summary>
        /// Return the EmailEntry object by email id.
        /// </summary>
        /// <param name="emailId"></param>
        /// <returns></returns>
        public EmailEntry getById(String emailId)
        {
            EmailEntry e = null;
            foreach (EmailEntry entry in m_EmailList)
            {
                if (entry.m_EmailId.Equals(emailId, StringComparison.InvariantCultureIgnoreCase))
                {
                    e = entry;
                    break;
                }
            }
            return e;
        }
    }
}
